<?php
	/*** By Weerachai Nukitram ***/
	/***  http://www.ThaiCreate.Com ***/
include("../connect/connect.php");
include('../function/function.php');

$dmy = $_POST["CheckIn"];
$d = substr($dmy, 0, 2);
$m = substr($dmy, 3, 2);
$y = substr($dmy, 6, 4);
$CheckIn = $y . '-' . $m . '-' . $d;

$dmy2 = $_POST["CheckOut"];
$d2 = substr($dmy2, 0, 2);
$m2 = substr($dmy2, 3, 2);
$y2 = substr($dmy2, 6, 4);
$CheckOut = $y2 . '-' . $m2 . '-' . $d2;

$DateDiff = DateDiff($CheckIn,$CheckOut);

$strSQL = "SELECT * FROM room ORDER BY room_name ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<!-- แสดงรายการประเภทห้อง -->
<h3>สรุปรายการห้องพัก</h3>
<table class="list">
<thead>
  <tr>
    <td class="center" width="120px">ประเภทห้อง</td>
<?php 
	$count = 90/$DateDiff;
for($i=0;$i<$DateDiff;$i++){
	$nextday1 = date("d/m/Y", strtotime("+".$i." days",strtotime($CheckIn)));
?>
    <td class="center" width="<?php echo $count;?>%"><?php echo $nextday1;?></td>
<?php } ?>
  </tr>
</thead>
<?php
//แสดงรายการประเภทห้อง
$objQuery2 = mysql_query("SELECT * FROM roomtype ORDER BY roomtype_name");
while($objResult2 = mysql_fetch_array($objQuery2))
{
	$iLoop1++;
	if(($iLoop1%2)=='0'){
		$bgcolor1 = "#F7F7FF";
	} else {
		$bgcolor1 = "";
	}
	$roomtype_id = $objResult2["roomtype_id"];
	$roomtype_name = $objResult2["roomtype_name"];
?>
  <tr bgcolor="<?php echo $bgcolor1;?>">
    <td class="center"><?php echo "<b>".$roomtype_name."</br>";?></td>
<?php 
for($i=0;$i<$DateDiff;$i++){
	$dayCheck = date("Y-m-d", strtotime("+".$i." days",strtotime($CheckIn)));
?>
    <td class="center"><?php 
		$cQuery = mysql_query("SELECT roomtype_id FROM room WHERE roomtype_id = '$roomtype_id' ");
		$cNum = mysql_num_rows($cQuery);
		$bQuery = mysql_query("SELECT reservation_id FROM reservation,room WHERE room.room_id = reservation.room_id AND reservation.reservation_checkin = '$dayCheck' AND room.roomtype_id = '$roomtype_id'");
		$bNum = mysql_num_rows($bQuery);
		$rFree = $cNum-$bNum;
		echo $rFree." ห้อง";
    ?></td>
<?php } ?>
  </tr>
<?php
}
?>
</table>
<hr size="1">
<!-- รายการห้องทั้งหมด -->
<h3>แสดงรายการห้องพักทั้งหมดของวันที่</h3>
<div align="right" style="padding-bottom: 10px;"><a class="button" onclick="return check(RoomSelect);"><span>จองห้องพัก</span></a></div>
<form name="RoomSelect" id="RoomSelect" method="post" action="roomselect.php">
<table class="list">
<thead>
  <tr>
    <td class="center" width="120px">เลขห้อง</td>
<?php 
for($i=0;$i<$DateDiff;$i++){
	
	$nextday1 = date("d/m/Y", strtotime("+".$i." days",strtotime($CheckIn)));
?>
    <td class="center" width="<?php echo $count;?>%"><?php echo $nextday1;?></td>
<?php } ?>
  </tr>
</thead>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
	$iLoop++;
	if(($iLoop%2)=='0'){
		$bgcolor = "#F7F7FF";
	} else {
		$bgcolor = "";
	}
	$roomname = $objResult["room_name"];
	$roomid = $objResult["room_id"];
	//$roompax = $objResult["room_pax"];
?>
  <tr bgcolor="<?php echo $bgcolor;?>">
    <td class="center"><?php echo "<b>".$roomname."</br>";?></td>
<?php 
for($i=0;$i<$DateDiff;$i++){
	$nextday2 = date("Y-m-d", strtotime("+".$i." days",strtotime($CheckIn)));
	$dayid = date("Ymd", strtotime("+".$i." days",strtotime($CheckIn)));
	$dQuery = mysql_query("SELECT reservation.room_id as room_id,reservation.reservation_checkin as reservation_checkin,reservation.reservation_status as reservation_status,reservation_detail.reservation_detail_fname as reservation_detail_fname,reservation_detail.reservation_detail_lname as reservation_detail_lname FROM reservation,reservation_detail WHERE reservation.reservation_detail_id = reservation_detail.reservation_detail_id  AND reservation.room_id = '".$objResult['room_id']."' AND reservation.reservation_checkin = '".$nextday2."' ");
	//$dRow = mysql_num_rows($dQuery);
	$dRows = mysql_fetch_array($dQuery);
	if($dRows['reservation_status']=='0'){
		$dShow = "<b>จอง<br>".$dRows['reservation_detail_fname']." ".$dRows['reservation_detail_lname']."</b>";
		$bgclo = "yes";
		$bgcolor = "#FFFF66";
	} else if($dRows['reservation_status']=='1'){
		$dShow = "<b>จอง Comfirm แล้ว<br>".$dRows['reservation_detail_fname']." ".$dRows['reservation_detail_lname']."</b>";
		$bgclo = "yes";
		$bgcolor = "#FF9933";
	} else if($dRows['reservation_status']=='2'){
		$dShow = "<b>ไม่ว่าง<br>".$dRows['reservation_detail_fname']." ".$dRows['reservation_detail_lname']."</b>";
		$bgclo = "yes";
		$bgcolor = "#FF0000";			
	} else {
		$dShow = "<label><input type=\"checkbox\" id=\"rCheck_".$objResult['room_id']."_".$dayid."\" name=\"room[]\" value=\"$roomid#$nextday2\"/> เลือก</label";	
		$bgclo = "no";
		//$bgcolor = "#33CC33";
	}
/*		
	if($dRow!='0'){
		$dShow = "<b>ไม่ว่าง<br>".$dRows['cus_name']."</b>";
		$bgclo = "yes";
	} else {
		$dShow = "<label><input type=\"checkbox\" name=\"room[]\" value=\"$roomid#$nextday2\"/> เลือก</label";	
		$bgclo = "no";
	}
*/
?>
    <td class="center" <?php if($bgclo=='no'){?>OnMouseOver="this.className='overmouse center'" onmouseout="this.className='center'"<?php } ?> <?php if($bgclo=='no'){?>onclick="rCheckBox('rCheck_<?php echo $objResult['room_id']."_".$dayid;?>');"<?php }?> style="background-color: <?php if($bgclo=='yes'){ echo $bgcolor;}// else {echo $bgcolor;}?>"><?php echo $dShow;?></td>
<?php } ?>
  </tr>
<?php
}
?>
</table>
</form>

<?php mysql_close(); ?>